package com.petshop.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.common.constant.BalzLogger;
import com.common.constant.ProjectConstant;
import com.common.constant.URLConstant;
import com.common.controller.BalzMD5Generate;
import com.petshop.model.Shop;
import com.petshop.model.User;
import com.petshop.model.UserRole;
import com.petshop.service.ShopService;
import com.petshop.service.ShopServiceImpl;
import com.petshop.service.UserProfileService;
import com.petshop.service.UserProfileServiceImpl;

public class LoginServlet extends HttpServlet {
	
	BalzLogger logger = new BalzLogger();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doPost(request, response); // if request in method get >> do Post
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		    
		HttpSession session = request.getSession();
		
		String loginUsername = (String) session.getAttribute("loginUsername");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String page = request.getParameter("page");
		
		logger.debug("loginUsername > "+loginUsername);
		logger.debug("username >"+username);
		logger.debug("password > "+password);
		logger.debug("Request Page > "+page);
		
		Integer userlogin = ProjectConstant.ROLE_LOCAL_USER ; 
		
		User userProfile = new User();
		UserRole role = new UserRole();
		
		// STEP_01 : Validate Check UserName & Password
		if( username != null && password != null && !username.equalsIgnoreCase("") && !password.equalsIgnoreCase("")){
			
			BalzMD5Generate generate = new BalzMD5Generate();
			password = generate.generatePasswordMD5(password).trim();
			logger.debug("MD5 Password > "+password);
			
			logger.debug("password MD5 > "+password);
			UserProfileService uService = new UserProfileServiceImpl() ;
			userlogin = uService.checkUserNamePassword(username, password);

			if(ProjectConstant.ROLE_ADMINISTRATOR == userlogin || ProjectConstant.ROLE_LOGIN_USER == userlogin ){
				session.setAttribute("loginUsername", username); 
				logger.debug("Session loginUsername > "+username);
				
				
				session.setAttribute("userRole", userlogin.toString()); 
				role.setRoleName(userlogin.toString());
				
				userProfile.setUserName(username);
				userProfile.setUserRole(role);
				
				page = URLConstant.INDEX;
				
			}else{
				session.invalidate();
			}
		
			logger.debug("userlogin Role > "+userlogin);
		
		}
		
		
		// STEP_02 : Check Request
		if (page == null ) {
			page = URLConstant.PAGE_LOGIN;
		} else {
			if(URLConstant.DETAILS.equalsIgnoreCase(page)){
				if(session.getAttribute("loginUsername")== null){
					page = URLConstant.PAGE_LOGIN;
				}else{
					page = URLConstant.PATH + page + URLConstant.JSP_FILE;
				}
			}else{
				page = URLConstant.PATH + page + URLConstant.JSP_FILE;
			}
		}
		
		logger.debug("Path 01 > "+page);		
		
		request.setAttribute("userProfile", userProfile);
		
		// Check That Request Page
		checkRequestPage(request , response , page);
		
	}
	
	protected void checkRequestPage(HttpServletRequest request, HttpServletResponse response, String page) throws IOException, ServletException{
		
		logger.debug("checkRequestPage > "+page);
		
		RedirectPage redirectPage = new RedirectPage();
		redirectPage.forward(request, response, page);
		
	}
	
	
	
}
